package com.ruoyi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.notice.domain.MgNotice;
import com.ruoyi.notice.service.IMgNoticeService;
import com.ruoyi.notice.service.INoticeUserService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 消息通知Controller
 * 
 * @author ruoyi
 * @date 2024-01-22
 */
@RestController
@RequestMapping("/notice/notice")
public class MgNoticeController extends BaseController
{
    @Autowired
    private IMgNoticeService mgNoticeService;
    
    private INoticeUserService noticeUserService;
    
    @Autowired
    public void setNoticeUserService(INoticeUserService noticeUserService) {
    	this.noticeUserService = noticeUserService;
    }

    /**
     * 查询消息通知列表
     */
    @PreAuthorize("@ss.hasPermi('notice:notice:list')")
    @GetMapping("/list")
    public TableDataInfo list(MgNotice mgNotice)
    {
        startPage();
        List<MgNotice> list = mgNoticeService.selectList(mgNotice);
        return getDataTable(list);
    }

    /**
     * 导出消息通知列表
     */
    @PreAuthorize("@ss.hasPermi('notice:notice:export')")
    @Log(title = "消息通知", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MgNotice mgNotice)
    {
        List<MgNotice> list = mgNoticeService.selectList(mgNotice);
        ExcelUtil<MgNotice> util = new ExcelUtil<MgNotice>(MgNotice.class);
        util.exportExcel(response, list, "消息通知数据");
    }

    /**
     * 获取消息通知详细信息
     */
    @PreAuthorize("@ss.hasPermi('notice:notice:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(mgNoticeService.selectById(id));
    }

    /**
     * 新增消息通知
     */
    @PreAuthorize("@ss.hasPermi('notice:notice:add')")
    @Log(title = "消息通知", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional
    public AjaxResult add(@RequestBody MgNotice mgNotice)
    {
        int insert = mgNoticeService.insert(mgNotice);
        
        int[] users = mgNotice.getUsers();
        
        if (null == users || 0 == users.length) {
        	return toAjax(insert);
        }
        
        this.noticeUserService.insertNoticeUser(users, insert);
        
		return toAjax(insert);
    }

    /**
     * 修改消息通知
     */
    @PreAuthorize("@ss.hasPermi('notice:notice:edit')")
    @Log(title = "消息通知", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional
    public AjaxResult edit(@RequestBody MgNotice mgNotice)
    {
        int update = mgNoticeService.update(mgNotice);
        
        
        this.noticeUserService.insertNoticeUser(mgNotice.getUsers(), mgNotice.getId());
        
		return toAjax(update);
    }

    /**
     * 删除消息通知
     */
    @PreAuthorize("@ss.hasPermi('notice:notice:remove')")
    @Log(title = "消息通知", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(mgNoticeService.deleteByIds(ids));
    }
}
